﻿Ext.namespace("Swfu");
Ext.namespace("Swfu.BackMgr");

Swfu.BackMgr.UserSelfMgr = Ext.extend(Ext.grid.EditorGridPanel,
{
    initComponent: function () {
        var sm = new Ext.grid.CheckboxSelectionModel();
        var fm = Ext.form;
        var cm = new Ext.grid.ColumnModel([new Ext.grid.RowNumberer(), sm,
        {
            header: "用户名", sortable: true, dataIndex: 'Name', readOnly: true
        }, {
            header: "手机", sortable: true, dataIndex: 'Mobile', editor: new fm.TextField({ allowBlank: false })
        }, {
            header: "分机", sortable: true, dataIndex: 'TelExt', editor: new fm.TextField({ allowBlank: false })
        }, {
            header: "短号", sortable: true, dataIndex: 'ShortNo', editor: new fm.TextField({ allowBlank: true })
        }]);
        var reader = new Ext.data.JsonReader(
        {
            autoLoad: true, fields: [{
                name: 'Name'
            }, {
                name: 'Mobile'
            }, {
                name: 'TelExt'
            }, {
                name: 'ShortNo'
            }]
        });
        var ds = new Ext.data.Store(
        {
            scopte: this, url: '/EmpProfile.mvc/GetEmpBasicInfo', reader: reader
        });
        Ext.apply(this,
        {
            id: 'UserSelfMgr', plain: true, layout: 'fit', border: false, cm: cm, sm: sm, frame: true,
            store: ds, loadMask: true, clicksToEdit: 1, tbar: [
            {
                text: "重新加载数据", iconCls: 'icon-redo',
                handler: function () {
                    ds.reload()
                }
                .createDelegate(this)
            }, '-', {
                text: "保存", iconCls: 'icon-save',
                handler: function () {
                    var record;
                    var grid = this.getStore();
                    grid.each(function (rs) {
                        if (rs.dirty) {
                            record = rs;
                            this.jsonData = Ext.util.JSON.encode(record.data);
                            Ext.Ajax.request(
                            {
                                url: '/EmpProfile.mvc/SaveEmpBasicInfo',
                                success: function () {
                                    Ext.ux.Toast.msg('提示', '已经成功保存修改的数据。');
                                    ds.reload()
                                },
                                failure: function (action) {
                                    if (action.failureType == 'server') {
                                        obj = Ext.util.JSON.decode(action.response.responseText);
                                        Ext.Msg.alert('错误:', obj.errors.reason)
                                    }
                                },
                                params: {
                                    json: this.jsonData
                                },
                                waitMsg: '保存数据中...'
                            })
                        }
                        else { }
                    });
                    grid.commitChanges()
                }
                .createDelegate(this)
            }], loadMask: true
        });
        Swfu.BackMgr.UserSelfMgr.superclass.initComponent.apply(this, arguments);
        ds.load();
    }
});

Ext.reg('UserSelfMgr', Swfu.BackMgr.UserSelfMgr);